package com.yang.doctorrecruitment.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yang.doctorrecruitment.dao.JobDAO;
import com.yang.doctorrecruitment.dto.ReleaseJob;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class ReleaseJobAction extends ActionSupport {
    private ReleaseJob releasejob;

    public String ReleaseJob(){
        String JobDuty=releasejob.getJobDuty();
        String JobName=releasejob.getJobName();
        int JobSalary=releasejob.getJobSalary();
        String JobAddress=releasejob.getJobAddress();
        String JobRequire=releasejob.getJobRequire();
        int JobNum=releasejob.getJobNum();
        Date date=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String PublishTime=df.format(new Date());
        //获得当前时间
        ActionContext ac=ActionContext.getContext();
        Map session=ac.getSession();
//        判断一下是否有jobID;

        String UserID= (String) session.get("UserID");
        //获得UserID
        ReleaseJob job=new ReleaseJob();
        job.setJobName(JobName);
        job.setJobAddress(JobAddress);
        job.setJobNum(JobNum);
        job.setJobRequire(JobRequire);
        job.setJobSalary(JobSalary);
        job.setPublishTime(PublishTime);
        job.setUserID(UserID);
        job.setStaticWords(JobName+JobAddress);
        job.setJobDuty(JobDuty);
        //获取招聘相关信息
        boolean res;
        HttpServletRequest request= (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);

        if(session.get("currentID")!=null) {
            String JobID= (String) session.get("currentID");
            System.out.println("JobID is "+JobID );
            res=JobDAO.updataJob(job,JobID);
        }else{
            System.out.println("JobID is null" );
            res = JobDAO.insertJob(job);
        }
        if(res){

            request.setAttribute("tipMessage",3);
            return SUCCESS;
        }else{
            request.setAttribute("tipMessage",4);
            return ERROR;
        }



    }
    public ReleaseJob getReleasejob() {
        return releasejob;
    }

    public void setReleasejob(ReleaseJob releasejob) {
        this.releasejob = releasejob;
    }
}
